using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SMSSender.Entities.Models;
using SMSSender.Entities.Utils;
using SMSSender.Common;

namespace SMSSender.DAL.Base
{
	public class CustomerDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public CustomerDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Customer table.
		/// </summary>
		public virtual void Insert(CustomerInfo customerInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPhone", customerInfo.IdPhone),
				new SqlParameter("@PhoneNumber", customerInfo.PhoneNumber),
				new SqlParameter("@NameCustomer", customerInfo.NameCustomer),
				new SqlParameter("@Address", customerInfo.Address)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Customer_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the Customer table.
		/// </summary>
		public virtual void Update(CustomerInfo customerInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPhone", customerInfo.IdPhone),
				new SqlParameter("@PhoneNumber", customerInfo.PhoneNumber),
				new SqlParameter("@NameCustomer", customerInfo.NameCustomer),
				new SqlParameter("@Address", customerInfo.Address)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Customer_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the Customer table by its primary key.
		/// </summary>
		public virtual void Delete(string idPhone)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPhone", idPhone)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Customer_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the Customer table.
		/// </summary>
		public virtual CustomerInfo Select(string idPhone)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPhone", idPhone)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Customer_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCustomerInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the Customer table.
		/// </summary>
		public List<CustomerInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Customer_SelectAll"))
			{
				List<CustomerInfo> customerInfoList = new List<CustomerInfo>();
				while (dataReader.Read())
				{
					customerInfoList.Add(MakeCustomerInfo(dataReader));
				}

				return customerInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the Customer class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CustomerInfo MakeCustomerInfo(SqlDataReader dataReader)
		{
			CustomerInfo customerInfo = new CustomerInfo();
			customerInfo.IdPhone = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMER.IDPHONE);
			customerInfo.PhoneNumber = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMER.PHONENUMBER);
			customerInfo.NameCustomer = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMER.NAMECUSTOMER);
			customerInfo.Address = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMER.ADDRESS);

			return customerInfo;
		}

		#endregion
	}
}
